<template>
  <div class="ele-body">
       <!-- 搜索表单 -->
  
    <el-card shadow="never">
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
    
      >
        <!-- 表头工具栏 -->
    
       <template v-slot:image_urls="{ row }">
       
            <el-avatar  v-if="row.image_urls" :src="row.image_urls[0]" shape="square" :size="150" />
            
       
            <el-avatar  v-if="row.image_urls" :src="row.image_urls[1]" shape="square" :size="150" />
        </template>
      
  <template v-slot:img="{ row }">
       
            <el-avatar  v-if="row.return_img" :src="row.return_img" shape="square" :size="200" />
            
       
        </template>
           
        <template v-slot:user="{ row }">
          <div class="ele-cell" v-if="row.figureUser">
            <el-avatar
              v-if="row.figureUser.avatar"
              :src="row.figureUser.avatar"
              shape="square"
              :size="40"
            />
            <div class="ele-cell-content">
              <div class="ele-cell-title">{{ row.figureUser.nickname }} </div>
              <div class="ele-cell-desc">{{ row.figureUser.telphone }} </div>
            </div>
          </div>
          <span v-else>无</span>
        </template>
      
      
       
       
            
    

        <!-- 状态列 -->
     


        
        
        <!-- 操作列 -->
        
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
 
  </div>
</template>

<script>


  import { faceTransplant  } from '@/api/intelligence/';

  import Sortable from 'sortablejs';

  export default {
    name: 'faceTransplant',
   
    data() {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center',
            fixed: 'left'
          },
         {
          prop: 'user',
          label: '用户',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'user'
        },
           {
            prop: 'title',
            label: '标题',
            showOverflowTooltip: true,
            minWidth: 110
          },
             {
            prop: 'image_urls',
            label: '模特图',
            showOverflowTooltip: true,
            minWidth: 110,
              minWidth: 110,slot: 'image_urls',
          },
           
             {
            prop: 'img',
            label: '结果',
            showOverflowTooltip: true,
            minWidth: 110,
            slot: 'img',
          },
          
      
  
   
        
    
          
        
      
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 是否显示分配回收员
        showCollector: false,
        //弹框名称
        id:0,
      };
    },
    methods: {
      rowDrop() {
        const tbody = document.querySelector('.el-table__body-wrapper tbody')
        const _this = this
        Sortable.create(tbody, {
          forceFallback: true,
          dragClass: 'drag',
          delay:100,
          animation: 1000,
          onEnd({ newIndex, oldIndex }) {
            console.log(newIndex);
            console.log(oldIndex);
            //更换表格数据 后续拖动坐标才会正确
            let data = _this.$refs.table.getData();
             
            let nid = data[newIndex].id;
            let oid = data[oldIndex].id;

            
            
            const currRow = data.splice(oldIndex, 1)[0];
            data.splice(newIndex, 0, currRow);

            sortChange(data).then((msg) => {
              
            }).catch((e) => {
              _this.$refs.table.reload();
            });
            //去数据库更换排序
            console.log(_this.$refs.table.getData());
          }
        })
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return faceTransplant({ ...where, ...order, page, limit });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      /* 打开编辑弹窗 */
      openEdit(row) {
        this.current = row;
        this.showEdit = true;
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({ lock: true });
        remove(row.id).then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
      },

      /* 一键默认 */
      doDefault() {
        
        this.$confirm('请谨慎使用该功能，一旦使用则会变成系统默认内容。确定一键默认？', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$loading({ lock: true });
          doDefault().then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => {});
      },
      
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除选中的数据吗?', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$loading({ lock: true });
          remove(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        }).catch(() => {});
      },
      
      /* 更改状态 */
      editStatus(row) {
        const loading = this.$loading({ lock: true });
        modify({id:row.id,field:'status',value: row.status})
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            row.status = row.status == 1 ? 2 : 1;
            this.$message.error(e.message);
          });
      },
       /* 更改状态 */
       editRecommend(row) {
        const loading = this.$loading({ lock: true });
        modify({id:row.id,field:'is_recommend',value: row.is_recommend})
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            row.is_recommend = row.is_recommend  == 1 ? 2 : 1;
            this.$message.error(e.message);
          });
      }
    },mounted() {
      this.rowDrop()
      //this.columnDrop()
    }
  };
</script>

<style>
 .drag {
    background: #000 !important;
    background-image: linear-gradient(#333, #999) !important;
  }
</style>
